package edu.dali.enterprise_test.kits;

import edu.dali.enterprise_test.kits.MapComparer.AscComparator;
import edu.dali.enterprise_test.kits.MapComparer.DescComparator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class ParamKits {

    private final static Logger LOGGER = LoggerFactory.getLogger(ParamKits.class);
    /**
     * 解析出url请求的路径，包括页面
     * @param strURL url地址
     * @return url路径
     */
    public static String UrlPage(String strURL)
    {
        String strPage=null;
        String[] arrSplit=null;

        strURL=strURL.trim().toLowerCase();

        arrSplit=strURL.split("[?]");
        if(strURL.length()>0)
        {
            if(arrSplit.length>1)
            {
                if(arrSplit[0]!=null)
                {
                    strPage=arrSplit[0];
                }
            }
        }

        return strPage;
    }
    /**
     * 去掉url中的路径，留下请求参数部分
     * @param strURL url地址
     * @return url请求参数部分
     */
    private static String TruncateUrlPage(String strURL)
    {
        String strAllParam=null;
        String[] arrSplit=null;

        strURL=strURL.trim().toLowerCase();

        arrSplit=strURL.split("[?]");
        if(strURL.length()>1)
        {
            if(arrSplit.length>1)
            {
                if(arrSplit[1]!=null)
                {
                    strAllParam=arrSplit[1];
                }
            }
        }

        return strAllParam;
    }
    /**
     * 解析出url参数中的键值对
     * 如 "index.jsp?Action=del&id=123"，解析出Action:del,id:123存入map中
     * @param URL  url地址
     * @return  url请求参数部分
     */
    public static Map<String, String> URLRequest(String URL)
    {
        Map<String, String> mapRequest = new LinkedHashMap<String, String>();

        String[] arrSplit=null;

        //String strUrlParam=TruncateUrlPage(URL);
        String strUrlParam= URL;
        if(strUrlParam==null)
        {
            return mapRequest;
        }
        //每个键值为一组 www.2cto.com
        arrSplit=strUrlParam.split("[&]");
        for(String strSplit:arrSplit)
        {
            String[] arrSplitEqual=null;
            arrSplitEqual= strSplit.split("[=]");

            //解析出键值
            if(arrSplitEqual.length>1)
            {
                //正确解析
                mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);

            }
            else
            {
                if(arrSplitEqual[0]!="")
                {
                    //只有参数没有值，不加入
                    mapRequest.put(arrSplitEqual[0], "");
                }
            }
        }
        return mapRequest;
    }


    /**
     * 组装字符串
     * @param params
     * @return
     */
    public static String buidParams(Map<String , String> params) throws UnsupportedEncodingException {
        String result = "";
        for(Map.Entry<String, String> entry : params.entrySet())
        {
            if (entry.getKey() != "sign")
                result +=entry.getKey()+"="+URLDecoder.decode(entry.getValue() , "UTF-8")+"&";
        }
        result = result.substring(0 , result.length()-1);
        LOGGER.info(result);
        return result;
    }

    /**
     * 组装字符串
     * @param params
     * @return
     */
    public static String buidParams(Map<String , String> params , boolean decode) throws UnsupportedEncodingException {
        String result = "";
        for(Map.Entry<String, String> entry : params.entrySet())
        {
            if (entry.getKey() != "sign")
                result +=entry.getKey()+"="+entry.getValue()+"&";
        }
        result = result.substring(0 , result.length()-1);
        LOGGER.info(result);
        return result;
    }


    /**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map , String  sort) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Comparator mComParator = null;
        if (sort.equals("asc"))
            mComParator = new AscComparator();
        else if (sort.equals("desc"))
            mComParator = new DescComparator();
        else
            throw new RuntimeException("排序参数错误");

        Map<String, String> sortMap = new TreeMap<String, String>(mComParator);

        sortMap.putAll(map);

        return sortMap;
    }

}
